//
//  AppDelegate.m
//  DriverBus
//
//  Created by MacMini20170218 on 2017/3/26.
//  Copyright © 2017年 JiYang. All rights reserved.
//
#define JPUSH_APP_KEY @"2d01bef276bc0456def68196"

#import "AppDelegate.h"
#import <AVFoundation/AVFoundation.h>
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate () <JPUSHRegisterDelegate>{
    BMKMapManager* _mapManager;
    NSUserDefaults *userDefaults;
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:@"forword" forKey:@"appStatu"];
    [self regidterNotification];
    [self setBaidu];
    self.driver = [[Driver alloc]initWithData];
    //判断是否已经登录，已经登录，直接到发车界面（注意：要用发车界面的Navigationcontroller）
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults boolForKey:DRIVER_LOG]){
        self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"StartNavigation"];
        return YES;
    }
    //设置极光推送
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption:launchOptions appKey:JPUSH_APP_KEY channel:@"App Store" apsForProduction:NO advertisingIdentifier:nil];
    return YES;
}

- (void)applicationWillEnterForeground:(UIApplication *)application{
    NSLog(@"applicationWillEnterForeground");
    //重新回来后，归还时间
    [[UIApplication sharedApplication] endBackgroundTask:UIBackgroundTaskInvalid];
    [userDefaults setObject:@"forword" forKey:@"appStatu"];
    //进入前台取消应用消息图标
    [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];
}

- (void)applicationDidEnterBackground:(UIApplication *)application{
    NSLog(@"applicationDidEnterBackground");
    [userDefaults setObject:@"back" forKey:@"appStatu"];
    //进入后台和系统借用时间
    [[UIApplication sharedApplication]beginBackgroundTaskWithExpirationHandler:nil];
}

#pragma mark 注册通知权限 iOS8以后必须需要用户授权才可以发送通知
- (void) regidterNotification{
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}

#pragma mark 初始化百度地图
- (void)setBaidu{
    //配置百度地图
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定generalDelegate参数
    BOOL ret = [_mapManager start:@"y1ELVKXDEuxfzPGvHX21N7A8doBDHHAw"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}

#pragma mark 极光配置
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    /// Required - 注册 DeviceToken
    [JPUSHService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}

- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
    
}


@end
